package com.yst.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.yst.common.core.domain.TreeSelect;
import com.yst.common.core.domain.entity.SysDept;
import com.yst.common.utils.DateUtils;
import com.yst.common.utils.StringUtils;
import com.yst.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yst.system.mapper.BasItemTypeMapper;
import com.yst.system.domain.BasItemType;
import com.yst.system.service.IBasItemTypeService;

/**
 * 产品类型Service业务层处理
 *
 * @author hoj
 * @date 2024-05-10
 */
@Service
public class BasItemTypeServiceImpl implements IBasItemTypeService {
    @Autowired
    private BasItemTypeMapper basItemTypeMapper;

    /**
     * 查询产品类型
     *
     * @param itemTypeId 产品类型主键
     * @return 产品类型
     */
    @Override
    public BasItemType selectBasItemTypeByItemTypeId(Long itemTypeId) {
        return basItemTypeMapper.selectBasItemTypeByItemTypeId(itemTypeId);
    }

    /**
     * 查询产品类型列表
     *
     * @param basItemType 产品类型
     * @return 产品类型
     */
    @Override
    public List<BasItemType> selectBasItemTypeList(BasItemType basItemType) {
        return basItemTypeMapper.selectBasItemTypeList(basItemType);
    }

    /**
     * 新增产品类型
     *
     * @param basItemType 产品类型
     * @return 结果
     */
    @Override
    public int insertBasItemType(BasItemType basItemType) {
        basItemType.setCreateTime(DateUtils.getNowDate());
        return basItemTypeMapper.insertBasItemType(basItemType);
    }

    /**
     * 修改产品类型
     *
     * @param basItemType 产品类型
     * @return 结果
     */
    @Override
    public int updateBasItemType(BasItemType basItemType) {
        basItemType.setUpdateTime(DateUtils.getNowDate());
        return basItemTypeMapper.updateBasItemType(basItemType);
    }

    /**
     * 批量删除产品类型
     *
     * @param itemTypeIds 需要删除的产品类型主键
     * @return 结果
     */
    @Override
    public int deleteBasItemTypeByItemTypeIds(Long[] itemTypeIds) {
        return basItemTypeMapper.deleteBasItemTypeByItemTypeIds(itemTypeIds);
    }

    /**
     * 删除产品类型信息
     *
     * @param itemTypeId 产品类型主键
     * @return 结果
     */
    @Override
    public int deleteBasItemTypeByItemTypeId(Long itemTypeId) {
        return basItemTypeMapper.deleteBasItemTypeByItemTypeId(itemTypeId);
    }

    @Override
    public List<TreeSelect> selectItemTypeTreeList(BasItemType itemType) {
        List<BasItemType> basItemTypes = selectBasItemTypeList(itemType);
        return buildBasItemTypeTreeSelect(basItemTypes);
    }

    private List<TreeSelect> buildBasItemTypeTreeSelect(List<BasItemType> basItemTypes) {
        List<BasItemType> basItemTypesTrees = buildBasItemTypeTree(basItemTypes);
        return basItemTypesTrees.stream().map(i -> new TreeSelect()).collect(Collectors.toList());

    }

    @Override
    public List<BasItemType> buildBasItemTypeTree(List<BasItemType> depts) {
        List<BasItemType> returnList = new ArrayList<BasItemType>();
        List<Long> tempList = depts.stream().map(BasItemType::getItemTypeId).collect(Collectors.toList());
        for (BasItemType dept : depts) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }
        return returnList;
    }

    private void recursionFn(List<BasItemType> list, BasItemType t) {
        // 得到子节点列表
        List<BasItemType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BasItemType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BasItemType> getChildList(List<BasItemType> list, BasItemType t) {
        List<BasItemType> tlist = new ArrayList<BasItemType>();
        Iterator<BasItemType> it = list.iterator();
        while (it.hasNext()) {
            BasItemType n = (BasItemType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getItemTypeId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BasItemType> list, BasItemType t) {
        return getChildList(list, t).size() > 0;
    }
}
